package net.yyge.manifest.editor;

import net.yyge.manifest.editor.tag.*;
import net.yyge.manifest.xml2axml.AxmlUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class AndroidManifest {
    private ManifestXml manifestXml;
    private String decode;


    /**
     * 将一个二进制的 AndroidManifest.xml 解析
     *
     * @param path
     * @return
     */
    public static AndroidManifest loadAXML(String path) {
        File file = new File(path);
        AndroidManifest editor = new AndroidManifest();
        try {
            editor.decode = AxmlUtils.decode(file);
            editor.manifestXml = new ManifestXml(editor.decode);
            return editor;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new RuntimeException("解析失败");
    }

    public static AndroidManifest loadAXML(byte[] data) {
        AndroidManifest editor = new AndroidManifest();
        try {
            editor.decode = AxmlUtils.decode(data);
            editor.manifestXml = new ManifestXml(editor.decode);
            return editor;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new RuntimeException("解析失败");
    }

    public String toXML() {
        return manifestXml.innerXML();
    }

    public ActivityTag getActivity() {
        return manifestXml.getActivity();
    }

    public ApplicationTag getApplication() {
        return manifestXml.getApplication();
    }

    public UsesPermissionTag getUsesPermission() {
        return manifestXml.getUsesPermission();
    }
    public PermissionTag getPermission() {
        return manifestXml.getPermission();
    }

    public ManifestTag getManifest() {
        return manifestXml.getManifest();
    }

    public UsesSdkTag getUsesSdk() {
        return manifestXml.getUsesSdk();
    }
    public ProviderTag getProvider() {
        return manifestXml.getProvider();
    }

    public byte[] toAXML(String xml) {
        return AxmlUtils.encode(xml);
    }

    public byte[] toAXML(File xmlFile) {
        return AxmlUtils.encode(xmlFile);
    }

    public File toAXML(String xml, File outFile) {
        byte[] axml = toAXML(xml);
        try (FileOutputStream fos = new FileOutputStream(outFile)) {
            fos.write(axml);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return outFile;
    }

    public File toAXML(File inXmlFile, File outFile) {
        byte[] axml = toAXML(inXmlFile);
        try (FileOutputStream fos = new FileOutputStream(outFile)) {
            fos.write(axml);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return outFile;
    }


    /**
     * 修改包名
     *
     * @param packageName 包名
     * @return this
     */
    public AndroidManifest setPackage(String packageName) {
        ManifestTag manifestTag = getManifest();
        manifestTag.setPackage(packageName);

        //修改所有含有包名的信息
        UsesPermissionTag usesPermission = getUsesPermission();
        usesPermission.updateNamePackage(manifestTag.getOldPackageName(), manifestTag.getPackage());


        //修改所有含有包名的信息
        PermissionTag permission = getPermission();
        permission.updateNamePackage(manifestTag.getOldPackageName(), manifestTag.getPackage());

        ProviderTag provider = getProvider();
        provider.updateNamePackage(manifestTag.getOldPackageName(), manifestTag.getPackage());
        return this;
    }

    /**
     * 应用使用的AndroidSdk编译版本号
     * @param num 使用的AndroidSdk编译版本号
     * @return this
     */
    public AndroidManifest setCompileSdkVersion(Integer num) {
        ManifestTag manifestTag = getManifest();
        manifestTag.setValue("compileSdkVersion", String.valueOf(num));
        return this;
    }

    /**
     * 设置AndroidSdk版本
     * @param min 最小版本号
     * @param target 目标版本号
     * @return this
     */
    public AndroidManifest setSdkVersion(Integer min, Integer target) {
        UsesSdkTag usesSdk = getUsesSdk();
        usesSdk.setValue("minSdkVersion", String.valueOf(min));
        usesSdk.setValue("targetSdkVersion",String.valueOf( target));
        return this;
    }


    /**
     * 添加权限
     * @param permissionName 权限名称 如：android.permission.WRITE_EXTERNAL_STORAGE
     * @return this
     */
    public AndroidManifest addPermission(String permissionName) {
        UsesPermissionTag usesPermission = getUsesPermission();
        usesPermission.add(permissionName);
        return this;
    }


    /**
     * 移除权限
     * @param permissionName 权限名称 如：android.permission.WRITE_EXTERNAL_STORAGE
     * @return this
     */
    public AndroidManifest removePermission(String permissionName) {
        UsesPermissionTag usesPermission = getUsesPermission();
        usesPermission.remove(permissionName);
        return this;
    }


    /**
     * 设置主Activity
     * @param activityName 主Activity名称
     * @return this
     */
    public AndroidManifest setMainActivity(String activityName) {
        ActivityTag activity = getActivity();
        activity.setMainActivity(activityName);
       return this;
    }




}
